package com.jack.cms.oss.core;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import com.jack.cms.oss.core.TalUploadService.CompressPicDemo;

public class FileUploadImage{
	private static final Logger log = LoggerFactory.getLogger(FileUploadImage.class);
	
  /** 
   * 改变图片的大小到宽为size，然后高随着宽等比例变化 
   * @param is 上传的图片的输入流 
   * @param os 改变了图片的大小后，把图片的流输出到目标OutputStream 
   * @param size 新图片的宽 
   * @param format 新图片的格式 
   * @throws IOException 
   */  
  public  InputStream resizeImage(InputStream is, int newWidth,int newHeight, String format,MultipartFile fileUpLoad) throws IOException {


      BufferedImage prevImage = ImageIO.read(is);  
      BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);  
      Graphics graphics = image.createGraphics();  
      graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);  
      ByteArrayOutputStream array = new ByteArrayOutputStream();  
      ImageIO.write(image, format, array);  
      InputStream imageInput = new ByteArrayInputStream(array.toByteArray());
     // fileUpLoad.
      return imageInput;

  }
  
  /**
   * input to file
   * @param ins
   * @param file
   */
	public  File inputstreamtofile(InputStream ins, File file) {
		try {
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			ins.close();
		} catch (Exception e) {
			log.info("异常信息为:" + e.getMessage());
		}
		return file;
	}

	/**
	 * 等比压缩生成7张不同规格的图片
	 * @param InputStream
	 * @return
	 * @throws Exception
	 */
	public Map<Integer,FileVO> imageProcee(InputStream is) throws Exception {
		Map<Integer,FileVO> images = new HashMap<Integer, FileVO>();
		/**
		 * 1：原图 、2：80*60、3：480*345、4：640x480、5：360*270、6：180*135、7：128*96
		 */
		try {

			// 保存6种压缩后的文件
			// 每种图的长宽
			byte[] buffer = IOUtils.toByteArray(is);
		    ByteArrayInputStream bai= new ByteArrayInputStream(buffer);
		   
			int newWidth = 0;
			int newHeight = 0;
			Integer index = 0;
		
			for (int i = 0; i < 6; i++) {
				switch (i) {
				//第一
				case 0:
					newWidth = 80;
					newHeight = 60;
					index = 2;
					images.put(index, compress(bai,newWidth,newHeight));
					break;
				case 1:
					newWidth = 480;
					newHeight = 345;
					index = 3;
					bai.reset();
					images.put(index, compress(bai,newWidth,newHeight));
					break;
				case 2:
					newWidth = 640;
					newHeight = 480;
					index = 4;
					bai.reset();
					images.put(index, compress(bai,newWidth,newHeight));
					break;
				case 3:
					newWidth = 360;
					newHeight = 270;
					index = 5;
					bai.reset();
					images.put(index, compress(bai,newWidth,newHeight));
					break;
				case 4:
					newWidth = 180;
					newHeight = 135;
					index = 6;
					bai.reset();
					images.put(index, compress(bai,newWidth,newHeight));
					break;
				case 5:
					newWidth = 128;
					newHeight = 96;
					index = 7;
					bai.reset();
					images.put(index, compress(bai,newWidth,newHeight));
					break;

				default:
					break;
				}
				
			}
			//重置流  存放原图
			bai.reset();
			FileVO vo = new FileVO();
		    vo.setFileSize(buffer.length/1024);
		    vo.setIn(bai);
		    //存放原图流
		    images.put(1, vo);
	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw e;
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
                    log.info("异常信息为:" + e.getMessage());
				}
			}
		}
		return images;
	}

	/**
	 * 根据图片流信息转为文件VO
	 * @param stream
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException
	 */
	protected  FileVO compress(InputStream stream ,int width,int height) throws IOException{
		CompressPicDemo  compressService = new CompressPicDemo();
		compressService.setWidthAndHeight(width, height);
		ByteArrayOutputStream bos = compressService.compressPic(stream);
		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		FileVO filevo = new FileVO();
		filevo.setFileSize(bos.size()/1024);
		filevo.setIn(bis);
		return filevo;
	}

	
}